//
//  SNMoreViewController.m
//  DayDayHappy
//
//  Created by yutao on 14-2-19.
//  Copyright (c) 2014年 yutao. All rights reserved.
//

#import "SNMoreViewController.h"
#import "KLSwitch.h"
#import "SDImageCache.h"
#import "UIAlertView+Blocks.h"
#import "SNAppDelegate.h"
//KLSwitch
#define kGreenColor [UIColor colorWithRed:66/255.0 green: 192/255.0 blue: 218/255.0 alpha: 1.0]



@interface SNMoreViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    BOOL isDay;
}
@property(nonatomic, strong)UITableView    *contentTableView;
@end

@implementation SNMoreViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
   
    [_contentTableView reloadData];

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIButton * dayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [dayBtn setFrame:CGRectMake(200, 5, 52/2, 52/2)];
    if ([USER_DEFAULT boolForKey:kIsHaveDayNightModle]) {
        BOOL isDayModle = [USER_DEFAULT boolForKey:kDayNightModle];
        isDay = isDayModle;
    }else{
        isDay = YES;
    }
    
    [dayBtn setBackgroundImage:[UIImage imageNamed:isDay ? @"night.png":@"day.png"] forState:UIControlStateNormal];
    [dayBtn addTarget:self action:@selector(dayModle:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * dayBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:dayBtn];
    self.navigationItem.rightBarButtonItem = dayBtnBtn;
    
    [self initContentTableView];
    
}

- (UIWindow *)brightnessWindow
{
    return [SNAppDelegate sharedAppDelegate].brightnessWindow;
}

-(void)dayModle:(UIButton *)sender{
    if (![USER_DEFAULT boolForKey:kIsHaveDayNightModle]) {
        [USER_DEFAULT setBool:YES forKey:kIsHaveDayNightModle];
    }
    
    if (isDay) {
        isDay = NO;
        [USER_DEFAULT setBool:isDay forKey:kDayNightModle];
        
        [sender setBackgroundImage:[UIImage imageNamed:@"day.png"] forState:UIControlStateNormal];
        [UIView animateWithDuration:0.5 animations:^{
            [self brightnessWindow].alpha = kNightValue;
        }];
    }else{
        isDay = YES;
        [USER_DEFAULT setBool:isDay forKey:kDayNightModle];
        [sender setBackgroundImage:[UIImage imageNamed:@"night.png"] forState:UIControlStateNormal];
        
        [UIView animateWithDuration:0.5 animations:^{
            [self brightnessWindow].alpha = kDayValue;
        }];
    }
    
}

- (void)initContentTableView
{
    self.contentTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        self.contentTableView.frame  =  CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds) - ( CGRectGetHeight(self.navigationController.navigationBar.frame)));
    }else{
        self.contentTableView.frame  =CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds) - ( CGRectGetHeight(self.navigationController.navigationBar.frame)) - ( CGRectGetHeight(self.tabBarController.tabBar.frame)) -20);
    }
    self.contentTableView.delegate = self;
    self.contentTableView.dataSource = self;
    
    [self.view addSubview:self.contentTableView];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 5;
    }else{
        return 2;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section ==1) {
        return 25;
    }else{
        return 0;
    }
}

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    if (section == 1) {
//        return [[UIView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, 25)];
//    }else{
//        return nil;
//    }
//    
//}

- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentifier = @"Cell";
    
    
    UITableViewCell * cell = [atableView dequeueReusableHeaderFooterViewWithIdentifier:cellIdentifier];
    if (cell== nil) {
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    [cell.textLabel setFont:[UIFont systemFontOfSize:17]];
    [cell.textLabel setTextColor:[UIColor colorWithRed:60/255.0 green:60/255.0 blue:60/255.0 alpha:1.0]];
    
    if (indexPath.section==0) {
        switch (indexPath.row) {
            case 0:
                [cell.textLabel setText:@"意见反馈"];
                break;
            case 1:
            {
                [cell.textLabel setText:@"评论同时转发"];
                KLSwitch * kswitch = [[KLSwitch alloc] initWithFrame:CGRectMake(230, 5, 125/2+10, 71/2) didChangeHandler:^(BOOL isOn) {
                    if (isOn) {
                        [USER_DEFAULT setObject:@YES forKey:@"isForward"];
                    }else{
                        [USER_DEFAULT setObject:@NO forKey:@"isForward"];
                    }
                }];
                [kswitch setOn:[USER_DEFAULT objectForKey:@"isForward"]?[[USER_DEFAULT objectForKey:@"isForward"] boolValue]:YES animated:NO];
                [kswitch setOnTintColor:kGreenColor];
                [cell addSubview:kswitch];

            }
                break;
            case 2:
                
            {
                [cell.textLabel setText:@"自动全屏模式"];
                KLSwitch * kswitch = [[KLSwitch alloc] initWithFrame:CGRectMake(230, 5, 125/2+10, 71/2) didChangeHandler:^(BOOL isOn) {
                    if (isOn) {
                        [USER_DEFAULT setObject:@YES forKey:@"isFullScreen"];
                    }else{
                        [USER_DEFAULT setObject:@NO forKey:@"isFullScreen"];
                    }
                }];
                [kswitch setOn:[USER_DEFAULT objectForKey:@"isFullScreen"]?[[USER_DEFAULT objectForKey:@"isFullScreen"] boolValue]:YES animated:NO];
                [kswitch setOnTintColor:kGreenColor];
                [cell addSubview:kswitch];
            }
                break;
            case 3:
                [cell.textLabel setText:[NSString stringWithFormat:@"清除缓存(已使用%.2fM)",([SDImageCache sharedImageCache].getSize)/1024.0/1024.0]];
                break;
            default:
                [cell.textLabel setText:@"打分支持我们"];
                break;
        }
    }else{
        if (indexPath.row==0) {
            [cell.textLabel setText:@"新浪微博"];
            UILabel * sinaLable = [[UILabel alloc] initWithFrame:CGRectMake(230, 5, 125/2+10, 71/2)];
            BOOL hasUserInfoSina = [[ShareClientUserInfo defaultShareClientUser] hasUserInfoWithServiceType:kShareClientServiceTypeWeiBo];
            if (hasUserInfoSina) {
//                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//                    ShareClientUserInfo *userInfo = [[ShareClientUserInfo defaultShareClientUser] userInfoWithServiceType:kShareClientServiceTypeWeiBo];
//                    dispatch_async(dispatch_get_main_queue(), ^{
//                        [sinaLable setText:userInfo.screenName];
//                    });
//                });
                ShareClientUserInfo *userInfo = [[ShareClientUserInfo defaultShareClientUser] userInfoWithServiceType:kShareClientServiceTypeWeiBo];
                [sinaLable setText:userInfo.screenName];
            }else{
                [sinaLable setText:@"未绑定"];
            }
            
            [cell addSubview:sinaLable];
        }else{
            
            [cell.textLabel setText:@"QQ空间"];
            UILabel * qqLable = [[UILabel alloc] initWithFrame:CGRectMake(230, 5, 125/2+10, 71/2)];
            BOOL hasUserInfoQQ = [[ShareClientUserInfo defaultShareClientUser] hasUserInfoWithServiceType:kShareClientServiceTypeQQZone];
            if (hasUserInfoQQ) {
//                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//                    ShareClientUserInfo *userInfo = [[ShareClientUserInfo defaultShareClientUser] userInfoWithServiceType:kShareClientServiceTypeWeiBo];
//                    dispatch_async(dispatch_get_main_queue(), ^{
//                        [qqLable setText:userInfo.screenName];
//                    });
//                });
               ShareClientUserInfo *userInfo = [[ShareClientUserInfo defaultShareClientUser] userInfoWithServiceType:kShareClientServiceTypeQQZone];
                [qqLable setText:userInfo.screenName];
            }else{
                [qqLable setText:@"未绑定"];
            }
            
            [cell addSubview:qqLable];
        }
    }
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"===cell.textLabel.text===%@",cell.textLabel.text);
    if (indexPath.section==0) {

        switch (indexPath.row) {
            case 0:
            {
//                FeedBackViewController * feedBack = [[FeedBackViewController alloc] init];
//                [feedBack setTitle:cell.textLabel.text];
//                feedBack.hidesBottomBarWhenPushed = YES;
//                [self.navigationController pushViewController:feedBack animated:YES];

            }
                break;
            case 3:
            {
                
                [UIAlertView showWithTitle:@"温馨提示" message:@"应用缓存占用空间%.2fM，清除缓存会导致您需要重新下载您所需要的内容，为了节省您的流量，我们不建议你清除缓存" cancelButtonTitle:@"取消" otherButtonTitles:[NSArray arrayWithObject:@"确定清除"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
                    if (buttonIndex==1) {
                    
                        
                    }
                }];
            }
                break;
            case 4:
            {
                //打分支持我们
            }
                break;
                
            default:
                break;
        }
        
        
        
    }else{
        if (indexPath.row == 0) {
            BOOL hasUserInfoQQ = [[ShareClientUserInfo defaultShareClientUser] hasUserInfoWithServiceType:kShareClientServiceTypeQQZone];
            if (hasUserInfoQQ) {
                [UIAlertView showWithTitle:@"温馨提示" message:@"您确定要解除绑定新浪微博绑定吗? 解除绑定后下次分享需要重新登录。" cancelButtonTitle:@"取消" otherButtonTitles:[NSArray arrayWithObject:@"确定"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
                    if (buttonIndex==1) {
                        [[ShareClientOAuthInfoSina defaultShareClientOAuth] deleteOAuthInfo:[[ShareClientOAuthInfoSina defaultShareClientOAuth] oAuthInfoWithServiceType:kShareClientServiceTypeWeiBo] serviceType:kShareClientServiceTypeWeiBo];
                        

                        [[ShareClientUserInfoQQ defaultShareClientUser] deleteUserInfo:[[ShareClientUserInfoQQ defaultShareClientUser] userInfoWithServiceType:kShareClientServiceTypeWeiBo] serviceType:kShareClientServiceTypeWeiBo];
                        
                    }
                }];
            }else{
                [[ShareCenterEngine defaultEngine] loginForServiceType:SNShareClientTypeSinaWeibo];
            }
            
        }else{
            BOOL hasUserInfoQQ = [[ShareClientUserInfo defaultShareClientUser] hasUserInfoWithServiceType:kShareClientServiceTypeQQZone];
            if (hasUserInfoQQ) {
                [UIAlertView showWithTitle:@"温馨提示" message:@"您确定要解除绑定QQ定吗? 解除绑定后下次分享需要重新登录。" cancelButtonTitle:@"取消" otherButtonTitles:[NSArray arrayWithObject:@"确定"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
                    if (buttonIndex==1) {
                        [[ShareClientOAuthInfoQQ defaultShareClientOAuth] deleteOAuthInfo:[[ShareClientOAuthInfoQQ defaultShareClientOAuth] oAuthInfoWithServiceType:kShareClientServiceTypeQQZone] serviceType:kShareClientServiceTypeQQZone];
                        
                        [[ShareClientUserInfoQQ defaultShareClientUser] deleteUserInfo:[[ShareClientUserInfoQQ defaultShareClientUser] userInfoWithServiceType:kShareClientServiceTypeQQZone] serviceType:kShareClientServiceTypeQQZone];
                        
                    }
                }];
            }else{
                [[ShareCenterEngine defaultEngine] loginForServiceType:SNShareClientTypeQZone];
            }
        }
    }
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
@end
